package com.situ.company.employee.service;

import java.util.List;

import com.situ.company.employee.dao.EmployeeDao;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.MD5;

public class EmployeeService {

    private EmployeeDao dao      = new EmployeeDao();
    private String      password = MD5.encode("123456");

    public String insert(EmployeeModel model) {
        if (selectModel(model) == null) {
            model.setPass(password);
            Integer res = dao.insert(model);
            return res == null ? null : res.toString();
        } else
            return "repeat";
    }

    public String delete(EmployeeModel model) {
        Integer res = dao.delete(model);
        return res == null ? null : res.toString();
    }

    public String update(EmployeeModel model) {
        Integer res = dao.update(model);
        return res == null ? null : res.toString();
    }

    public EmployeeModel selectModel(EmployeeModel model) {
        EmployeeModel m0 = new EmployeeModel(model.getCode());
        List<EmployeeModel> list = selectList(m0);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    public List<EmployeeModel> selectList(EmployeeModel model) {
        String code = model.getCode();
        if (code == null)
            code = "";
        model.setCode("%" + code + "%");
        String name = model.getName();
        if (name == null)
            name = "";
        model.setName("%" + name + "%");
        return dao.select(model);
    }

    public Integer selectCount(EmployeeModel model) {
        EmployeeModel m0 = new EmployeeModel();
        String code = model.getCode();
        if (code == null)
            code = "";
        m0.setCode("%" + code + "%");
        String name = model.getName();
        if (name == null)
            name = "";
        m0.setName("%" + name + "%");
        return dao.selectCount(m0);
    }

    public String login(EmployeeModel model) {
        EmployeeModel m0 = selectModel(model);
        if (m0 == null)
            return "0";
        String pass = model.getPass();
        pass = MD5.encode(pass);
        return m0.getPass().equals(pass) ? "1" : "2";
    }

    public String updatePassReset(EmployeeModel model) {
        model.setPass(password);
        Integer res = dao.updatePass(model);
        return res == null ? null : res.toString();
    }

    public String updatePass(EmployeeModel model) {
        model.setPass(MD5.encode(model.getPass()));
        Integer res = dao.updatePass(model);
        return res == null ? null : res.toString();
    }

    public String updateDept(EmployeeModel model) {
        Integer res = dao.updateDept(model);
        return res == null ? null : res.toString();
    }

    public String updateImage(EmployeeModel model) {
        Integer res = dao.updateImage(model);
        return res == null ? null : res.toString();
    }

}
